<template>
  <a-modal :title="title" :width="width" :visible="visible" @ok="handleOk" :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭">
    <LabWsuperviseForm ref="registerForm" @ok="submitCallback" :auditMark="auditMark" :formDisabled="disableSubmit" :formBpm="false"></LabWsuperviseForm>
    <template v-slot:footer>
      <div v-if="auditMark!=''">
        <a-button type="default" @click="handleCancel"> 取消 </a-button>
        <a-button type="danger" @click="showAuditModal('no')">不同意</a-button>
        <a-button type="primary" @click="showAuditModal('yes')">同意</a-button>
      </div>
      <div v-else-if="disableSubmit">
        <a-button type="default" @click="handleCancel">关闭</a-button>
      </div>
      <div v-else>
        <a-button type="default" @click="handleCancel">关闭</a-button>
        <a-button type="primary" @click="handleOk">确定</a-button>
      </div>
    </template>
    <AuditModal @register="regAuditModal" @ok1="closeModals"></AuditModal>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose } from 'vue';
  import LabWsuperviseForm from './LabWsuperviseForm.vue'
  import AuditModal from './FinalAuditModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useUserStore } from '/@/store/modules/user';
  const { userInfo } = useUserStore();
  
  const title = ref<string>('');
  const width = ref<number>(1050);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  let auditMark = ref('');
  const emit = defineEmits(['register', 'success']);


  /**
   * 新增
   */
  function add() {
    title.value = '新增';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add();
    });
  }
  
  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  const [regAuditModal, { openModal: openAuditModal, closeModal: closeAuditModal }] = useModal();
  async function showAuditModal(str) {
    let formData = registerForm.value.formData
    console.log('formData',formData);
    openAuditModal(true, { formData , str });
  }
  async function closeModals() {
        //延迟0.5秒关闭弹框
        setTimeout(() => {
          //关闭子modal
          closeAuditModal();
          //关闭父 modal
          handleCancel();
          //刷新列表
          emit('success');
        }, 1000);
      }
  
  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
    auditMark,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
